package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 查询角色信息
     * @return
     */
    @Override
    public List<Role> findAllRoles() {
        return roleDao.findAllRoles();
    }

    //分页查询
    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.fingByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total, rows);
    }


    //根据角色id查询角色信息
    @Override
    public Role findByRoleId(Integer id) {
        return roleDao.findByRoleId(id);
    }

    /**
     * 根据角色id和权限id  进行关联
     * @param roleId
     * @param permissionIds
     */
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //编辑
    @Override
    public void edit(Integer[] permissionIds, Role role) {
        Integer id = role.getId();
        //先删除角色和权限关联
        roleDao.deleRoleIdAndPermissionId(id);
        //添加角色和权限新的关联
        this.setRoleAndPermission(id, permissionIds);
        //编辑角色信息
        roleDao.edit(role);
    }

    //根据角色id删除
    @Override
    public void delete(Integer id) {
        //先删除角色和权限关联
        roleDao.deleRoleIdAndPermissionId(id);
        //在删除角色和菜单的关联
        roleDao.deleteRoleAndMenu(id);
        //在后来删除角色和用户的关联
        roleDao.deleteRoleAndUser(id);
        //最后删除角色信息
        roleDao.delete(id);
    }

    @Override
    public void add(Role role, Integer[] permissionIds) {
        //新增检查组，操作t_role表
        roleDao.add(role);
        //设置角色表和权限表的多对多的关联关系，操作t_role_permission表
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId, permissionIds);

    }

    //查询所有角色信息
    @Override
    public List<Role> findAllRole() {
        return roleDao.findAllRole();
    }
}
